function shouldRegister(doc) {

  var whitelist_jsp_pages = null;
  var whitelist_wicket_pages = null;
  if(this.getWhitelist_jsp_pages){
  	 whitelist_jsp_pages = getWhitelist_jsp_pages();
  }
  if(this.getWhitelist_wicket_pages){
     whitelist_wicket_pages = getWhitelist_wicket_pages();
  }
  
  if(whitelist_jsp_pages == null && whitelist_wicket_pages==null){
  	 //se whitelists não tiverem elementos então o aguarde deve entrar em todas as paginas
  	 return true;
  }

  var jsp = doc.find('.tdMainTitle').text();
  var wick = doc.find('.breadcrumb'); 

  if (jsp && whitelist_jsp_pages != null) {
    for (var i = 0; i < whitelist_jsp_pages.length; i++) {
      if (jsp.indexOf(whitelist_jsp_pages[i]) != -1) {
        return true;
      }
    }
  } else if (wick && whitelist_wicket_pages !=null) {
    for (var i = 0; i < whitelist_wicket_pages.length; i++) {
      if (wick.find(':contains(' + whitelist_wicket_pages[i] + ')').length > 0) {
        return true;
      }
    }
  }

  return false;
}

function shouldBlock(obj){
  var should_block = true;

  var blacklist_img = [
    'expand.gif',
    'collapse.gif',
    'mais.gif',
    'menos.gif',
    'adobe.gif',
    'adobe.png',
    'excel.png'
  ];

  var blacklist_classes = [
    'no_blockui'
  ];

  // Anchors
  if (obj.href && obj.href.indexOf('#') == obj.href.length - 1) {
    return false;
  }

  $(blacklist_img).each(function(i, v) {
    if (obj.src && obj.src.toLowerCase().indexOf(v) != -1) {
      should_block = false;
      return;
    }
  });

  $(blacklist_classes).each(function(i, v) {
    if (obj.className && obj.className.toLowerCase().indexOf(v) != -1) {
      should_block = false;
      return;
    }
  });

  return should_block;
}

function block_it() {
  if (!shouldBlock(this)) {
      return;
  }

  $.blockUI({
    fadeIn: 100,
    css: {
      fontFamily: 'Arial, Tahoma, Helvetica, sans-serif',
      fontSize: '15px',
      zIndex: '3000'
    },
    message: '<input type="button" class="button" style="float:right" value="X" onclick="$.unblockUI()" /><img src="../images/busy.gif" />Por favor, aguarde...<br />'
  });
}

$(document).ready(
 function() {
    if (shouldRegister($(document))) {
      $('a, :image, :submit, :button').click(block_it);
    }

    $('iframe').load(
	 function() {
	    j = parent.window.$;

	    var doc = j('iframe').contents();
        if (!shouldRegister(doc)) {
		  return;
		}

	    j.unblockUI();
	    doc.find(':submit, :image').click(block_it);
	 });
});
